{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "64fa4d2e-e1a1-4d16-b04b-0b73667b6733",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time out\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'WebDriver' object has no attribute 'find_element_by_css_selector'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_5824\\2189266345.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'time out'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;31m# print(browser.page_source)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_element_by_css_selector\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'h1.view-title'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'WebDriver' object has no attribute 'find_element_by_css_selector'"
     ]
    }
   ],
   "source": [
    "# fetch http://www.santostang.com/2018/07/04/hello-world/ by selenium\n",
    "from selenium import webdriver as Wd\n",
    "from selenium.common.exceptions import TimeoutException\n",
    "browser=Wd.Chrome()\n",
    "browser.set_page_load_timeout(10)\n",
    "try:\n",
    "    browser.get('http://www.santostang.com/2018/07/04/hello-world/')\n",
    "except TimeoutException:\n",
    "    print('time out')\n",
    "# print(browser.page_source)\n",
    "print(browser.find_element_by_css_selector('h1.view-title').text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3b0c1e49-7787-49c7-861e-3eda0784cc57",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一周之内，习近平会晤四位外国领导人\n",
      "“典”亮新征程丨治国有常，利民为本\n",
      "党代表回基层：共推高质量发展\n",
      "巴基斯坦博士后：用中国所学实现梦想\n",
      "留学生刘星：在中国所学让我信心满满\n",
      "今天才发现，微信长按2秒隐藏5大功能，很多人都还不知道，真好用\n",
      "多名学生还原教师网课爆破事件：被围攻几十分钟，猎手开麦辱骂\n",
      "60岁的梁朝伟，你在怕什么？\n",
      "“万亿俱乐部”大盘点：上海重回第一，重庆晋级四强，青岛追赶……\n",
      "2022英雄联盟全球总决赛DRX夺冠\n",
      "11月，你好\n",
      "为何“非典”会突然消失？新冠已经持续三年，何时才能结束？\n",
      "从星光大道冠军到“人人喊打”，盲人歌手杨光到底经历了什么？\n",
      "2003年上海最牛钉子户：因1亿拆迁款苦等14年，最终败给现实\n"
     ]
    }
   ],
   "source": [
    "# fetch home price from toutiao\n",
    "from selenium import webdriver as Wd\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "\n",
    "option = Wd.ChromeOptions()\n",
    "option.add_argument(\"headless\")\n",
    "browser=Wd.Chrome(options =option)\n",
    "# browser.set_page_load_timeout(10)\n",
    "browser.get('https://www.toutiao.com/?wid=1667749364146')\n",
    "time.sleep(3)\n",
    "# print(browser.page_source)\n",
    "all_news=browser.find_elements(By.CSS_SELECTOR,'div.feed-card-article-l')\n",
    "# print(all_news)\n",
    "for news in all_news:\n",
    "    print(news.find_element(By.CSS_SELECTOR,'a.title').text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
